package com.ymatou.iapi.godmch.testcase;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

import net.sf.json.JSONObject;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.common.collect.Lists;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.ymatou.iapi.godmch.parameter.getConditionMarkupConfigBean;
import com.ymatou.iapi.godmch.parameter.setConditionLevelBean;
import com.ymatou.iapi.godmch.parameter.setConditionMarkupConfigBean;
import com.ymatou.iapi.godmch.service.getConditionMarkupConfigCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmch.markup_config_condition;
import com.ymttest.database.model.godmch.markup_config_conditionExample;
import com.ymttest.database.sqlwapper.godmch.markup_config_conditionWapper;
import com.ymttest.utils.TestDataManager;
/**
*按条件自动加价配置详情
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getConditionMarkupConfig{
	private static getConditionMarkupConfigBean bean;
	private static getConditionMarkupConfigCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("按条件自动加价配置详情");
	}

	@Before
	public void caseUp() {
		bean=new getConditionMarkupConfigBean();
		call=new getConditionMarkupConfigCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private markup_config_condition selectConfig(Integer agentId,Integer supplierId,Integer... markupId) throws InterruptedException
	{
		markup_config_conditionWapper markupConfig=new markup_config_conditionWapper();
		markup_config_conditionExample example=new markup_config_conditionExample();
		example.createCriteria().andAgent_idEqualTo(agentId).andSupplier_idEqualTo(supplierId);
		if(markupId.length>0)
			example.createCriteria().andMarkup_idEqualTo(markupId[0]);
		
		example.setOrderByClause(" create_time desc");
		markup_config_condition config=markupConfig.selectByExample(example).get(0);
		return config;
	}
	private void updateTimeConfig(Integer agentId,Integer supplierId) throws InterruptedException
	{
		   //修改update_time 因为相同的agentid+suppelierId 1分钟内无法修改
		   markup_config_conditionWapper markupConfig=new markup_config_conditionWapper();
		   markup_config_conditionExample example=new markup_config_conditionExample();
		   example.createCriteria().andAgent_idEqualTo(agentId).andSupplier_idEqualTo(supplierId);
		   markup_config_condition config=markupConfig.selectByExample(example).get(0);
		   config.setUpdate_time(Date.from((LocalDateTime.now().plusDays(-1).atZone(ZoneId.systemDefault()).toInstant())));
		   markupConfig.updateByPrimaryKey(config);
	}
	private void check_verify(Integer agentId,Integer supplierId,markup_config_condition config) throws InterruptedException {
		JSONObject job= call.getResult();
		 config=selectConfig(agentId,supplierId,bean.getMarkupId());
		 Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		 Logger.verifyEquals(Integer.parseInt(job.get("agentId").toString()), config.getAgent_id(), "验证数据库中的agentId与返回的一致");
		 Logger.verifyEquals(Integer.parseInt(job.get("supplierId").toString()), config.getSupplier_id(), "验证数据库中的supplierId与返回的一致");
		 Logger.verifyEquals(Integer.parseInt(job.get("markupId").toString()), config.getMarkup_id(), "验证数据库中的markupId与返回的一致");
		 
		 //Logger.verifyEquals(job.get("brandList").toString().replace("[", "").replace("]", ""), 
		// config.getCondition_list().replace("{", "").replace("}", "").replace("[", "").replace("]", "").replace("brand", "").replace(":", "").replace("\"\"", ""), "验证数据库中的brandList与返回的一致");
		 
		 Logger.verifyEquals(Integer.parseInt(job.get("markupType").toString()), config.getMarkup_type(), "验证数据库中的Markup_type与返回的一致");
		 Logger.verifyEquals(new BigDecimal(job.get("percentage").toString()).stripTrailingZeros().toPlainString(), config.getPercentage().stripTrailingZeros().toPlainString(), "验证数据库中的percentage与返回的一致");
		 Logger.verifyEquals(Integer.parseInt(job.get("decimalType").toString()), config.getDecimal_type(), "验证数据库中的decimalType与返回的一致");
		 Logger.verifyEquals(Integer.parseInt(job.get("onSaleType").toString()), config.getOn_sale_type(), "验证数据库中的onSaleType与返回的一致");
		 Logger.verifyEquals(job.get("refreshProduct"), config.getRefresh_product(), "验证数据库中的refreshProduct与返回的一致");
		// Logger.verifyEquals(job.get("title").toString(), "品牌", "验证数据库中的title与返回的一致");

	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getConditionMarkupConfig_001() {
		Logger.start(true,"按条件自动加价配置详情-一条类型-返回成功");
		try {
			 Integer agentId=GodmchManageCallService.createAgent();
	         Integer supplierId=GodmchManageCallService.createApplySupplier().getSupplierId();
				//新增
	         setConditionMarkupConfigBean setBean=GodmchManageCallService.initSetConditionMarkupConfig(agentId,supplierId);
	         GodmchManageCallService.setConditionMarkupConfigCall(setBean);
	         //修改时间
	         updateTimeConfig(agentId,supplierId);
	         markup_config_condition config=selectConfig(agentId,supplierId);
	           
	         bean.setAgentId(agentId);
	         bean.setSupplierId(supplierId);
	         bean.setMarkupId(config.getMarkup_id());
			 call.setData(bean);
			 call.callService();			
			
			 check_verify(  agentId, supplierId,config);
			 
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getConditionMarkupConfig_002() {
		Logger.start(true,"按条件自动加价配置详情-设置两条类型-返回成功");
		try {
			 Integer agentId=GodmchManageCallService.createAgent();
	         Integer supplierId=GodmchManageCallService.createApplySupplier().getSupplierId();
				//新增
	         setConditionMarkupConfigBean setBean=GodmchManageCallService.initSetConditionMarkupConfig(agentId,supplierId);
	         
	         setBean.setCategoryList(Lists.newArrayList(Integer.parseInt(TestDataManager.getRandomNum(5)),Integer.parseInt(TestDataManager.getRandomNum(5))));
	         setBean.setDepotList(Lists.newArrayList(Integer.parseInt(TestDataManager.getRandomNum(5)),Integer.parseInt(TestDataManager.getRandomNum(5))));
	         GodmchManageCallService.setConditionMarkupConfigCall(setBean);
	         //修改时间
	         updateTimeConfig(agentId,supplierId);
	         markup_config_condition config=selectConfig(agentId,supplierId);
	           
	         bean.setAgentId(agentId);
	         bean.setSupplierId(supplierId);
	         bean.setMarkupId(config.getMarkup_id());
			 call.setData(bean);
			 call.callService();		
			
			 check_verify(  agentId, supplierId,config);
			 
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getConditionMarkupConfig_003() {
		Logger.start(true,"按条件自动加价配置详情-代理商id为空-返回失败");
		try {
			 Integer agentId=GodmchManageCallService.createAgent();
	         Integer supplierId=GodmchManageCallService.createApplySupplier().getSupplierId();
				//新增
	         setConditionMarkupConfigBean setBean=GodmchManageCallService.initSetConditionMarkupConfig(agentId,supplierId);
	         GodmchManageCallService.setConditionMarkupConfigCall(setBean);
	         //修改时间
	         updateTimeConfig(agentId,supplierId);
	         markup_config_condition config=selectConfig(agentId,supplierId);
	           
	        // bean.setAgentId(agentId);
	         bean.setSupplierId(supplierId);
	         bean.setMarkupId(config.getMarkup_id());
			 call.setData(bean);
			 call.callService();		
			 Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getConditionMarkupConfig_004() {
		Logger.start(true,"按条件自动加价配置详情-供货商id为空-返回失败");
		try {
			 Integer agentId=GodmchManageCallService.createAgent();
	         Integer supplierId=GodmchManageCallService.createApplySupplier().getSupplierId();
				//新增
	         setConditionMarkupConfigBean setBean=GodmchManageCallService.initSetConditionMarkupConfig(agentId,supplierId);
	         GodmchManageCallService.setConditionMarkupConfigCall(setBean);
	         //修改时间
	         updateTimeConfig(agentId,supplierId);
	         markup_config_condition config=selectConfig(agentId,supplierId);
	           
	         bean.setAgentId(agentId);
	         //bean.setSupplierId(supplierId);
	         bean.setMarkupId(config.getMarkup_id());
			 call.setData(bean);
			 call.callService();		
			 Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}